package org.xhnews.database;

import java.util.Vector;

import net.rim.device.api.database.Cursor;
import net.rim.device.api.database.Row;

import org.xhnews.log.RaspberryLogger;
import org.xhnews.to.City;
import org.xhnews.to.Contact;
import org.xhnews.to.MessageItem;
import org.xhnews.to.NewsChannel;
import org.xhnews.to.NewsItem;
import org.xhnews.to.Province;
import org.xhnews.to.RssChannel;
import org.xhnews.to.RssItem;
import org.xhnews.to.WeatherItem;
import org.xhnews.utils.FileUtil;
import org.xhnews.utils.StringUtil;

public class DatabaseManager {
	/*
	 * 初始化数据库
	 */
	public static void initDataBase() {
		RaspberryLogger.getInstance().info("initDataBase start");
		DataBase db=new DataBase();
		db.init();
		RaspberryLogger.getInstance().info("initDataBase OK.");
	}
    /*
     * 设置新闻为已读状态
     */
	public static boolean setItemRead(String itemId, boolean b) {
		DataBase db=new DataBase();
		db.open();
		boolean res = db.setItemRead(itemId, b);
		db.close();
		return res;

	}
	/*
	 * 删除频道
	 */
	public static boolean delChannel(String channelFileName) {
		DataBase db=new DataBase();
		db.open();
		boolean res = db.delChannel(channelFileName);
		db.close();
		return res;

	}
	/*
	 * 设置新闻为收藏状态
	 */
	public static boolean setItemFavor(String itemId, boolean b) {
		DataBase db=new DataBase();
		db.open();
		boolean res = db.setItemFavor(itemId, b);
		db.close();
		return res;
	}
	/*
	 * 设置频道为有效激活状态
	 */
	public static boolean setChannelEnable(String chName, boolean b) {
		DataBase db=new DataBase();
		db.open();
		boolean res = db.setChannelEnable(chName, b);
		db.close();
		return res;
	}
	/*
	 * 设置每个频道最大新闻条数
	 */
	public static boolean setChannelMaxNewsID(String chName, String maxid) {
		DataBase db=new DataBase();
		db.open();
		boolean res = db.setChannelMaxNewsID(chName, maxid);
		db.close();
		return res;

	}
	/*
	 * 插入新频道
	 */
	public static void insertNewsChannel(NewsChannel ch){
		DataBase db=new DataBase();
		db.open();
		db.insertChannel(ch.getFileName(),ch.getTitle(),ch.getMaxNewsID());
		db.close();
	}
	/*
	 * 删除未激活状态的频道的新闻
	 */
	public static void delNoChannelNews(){
		DataBase db=new DataBase();
		db.open();
		db.delNoChannelNews();
		db.close();
	}

    /*
     * 获得所有频道对象
     */
	public static Vector getAllNewsChannels() {
		DataBase db=new DataBase();
		db.open();
		Cursor cursor = db.getAllChannels();
		Vector channels = new Vector();
		Row row;
		try {
			while (cursor.next()) {
				row = cursor.getRow();
				String fileName = row.getString(0); 
				String title = row.getString(1);
				int enable = row.getInteger(2);
				String maxNewsId = row.getString(3);
				NewsChannel ch = new NewsChannel();
				ch.setFileName(fileName);
				ch.setTitle(title);
				ch.setEnable(enable > 0 ? true : false);
				ch.setMaxNewsID(maxNewsId);
				channels.addElement(ch);
			}
			db.close();
			return channels;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			e.printStackTrace();
			db.close();
			return null;
		}

	}
    /*
     * 获得所有处于激活状态的频道对象
     */
	public static Vector getAllEnableChannels() {
		DataBase db=new DataBase();
		db.open();
		Cursor cursor = db.getAllEnableChannels();
		if(cursor!=null){
		Vector channels = new Vector();	
		Row row;
		try {
			while (cursor.next()) {
				row = cursor.getRow();
				String fileName = row.getString(0);
				String title = row.getString(1);
				String maxNewsId = row.getString(3);
				NewsChannel ch = new NewsChannel();
				ch.setFileName(fileName);
				ch.setTitle(title);
				ch.setMaxNewsID(maxNewsId);
				channels.addElement(ch);
			}
			cursor.close();
			db.close();
			return channels;
		} catch (Exception e) {
			e.printStackTrace();
			RaspberryLogger.getInstance().info(e.getMessage());
		}
		}
		db.close();
		return null;
	}
    /*
     * 插入一个频道的多条新闻
     */
	public static void insertNewsItems(Vector items, String channelID, int limit,String path) {
		DataBase db=new DataBase();
		db.open();
		int length = items.size();		
		for (int i = 0; i < length; i++) {
			NewsItem item = (NewsItem) items.elementAt(i);
			item.setChannelFileName(channelID);
			db.insertItem(item.getID(), item.getFileName(), item
					.getTitle(), item.getPubDate(), item.getChannelFileName(),
					item.getAttach());
		}
		// 删除多余资讯
		int currentItemCount = db.getChannelItemCount(channelID);
		if (limit < currentItemCount) {
			// 需要删除旧的数据
			int del_cnt = currentItemCount - limit;
			Cursor cursor = db.getToDeleteNewsFileNames(channelID,
					del_cnt);
			Vector newsToDelList = new Vector();
			Row row;
			try {
				while (cursor.next()) {
					row = cursor.getRow();
					String id = row.getString(0);
					String fileName = row.getString(1);
					String attach = row.getString(2);
					NewsItem item = new NewsItem();
					item.setID(id);
					item.setFileName(fileName);
					item.setAttach(attach);
					newsToDelList.addElement(item);
				}
			} catch (Exception e) {
				RaspberryLogger.getInstance().info(e.getMessage());
				e.printStackTrace();
			}
			if (newsToDelList != null && newsToDelList.size() != 0) {
				for (int i = 0; i < newsToDelList.size(); i++) {
					NewsItem item = (NewsItem) newsToDelList.elementAt(i);
					// 删除数据库
					db.delNewsByID(item.getID());
					// 删除附件文件
					delNewsFileAndAttach(item,path);
				}
			}
		}
		db.close();
	}
    /*
     * 删除新闻附件文件
     */
	private static void delNewsFileAndAttach(NewsItem item, String path) {
		String attach = item.getAttach();
		if (!attach.equalsIgnoreCase("none")) {
			String[] subAttach = StringUtil.split(attach, ';');
			for (int i = 0; i < subAttach.length; i++) {
				int start = subAttach[i].lastIndexOf('/');
				int end = subAttach[i].lastIndexOf(':');
				if (start != -1 && end != -1) {
					String attachfilename = subAttach[i].substring(start, end);
					FileUtil.deleteFile(path, attachfilename);
				}
			}
		}
	}
	/*
	 * 获得最新的新闻列表
	 */
	public static Vector getLatestNews() {
		DataBase db=new DataBase();
		db.open();
		Cursor cursor = db.getLatestNews();
		Vector items = new Vector();
		Row row;
		try {
			while (cursor.next()) {
				row = cursor.getRow();
				String id = row.getString(0); 
				String fileName = row.getString(1);
				String title = row.getString(2);
				String pubDate = row.getString(3);
				int isRead = row.getInteger(4);
				String attach = row.getString(5);
				String channelTitle = row.getString(6);
				String channelFileName = row.getString(7);
				int isFavor = row.getInteger(8);
				NewsItem item = new NewsItem();
				item.setID(id);
				item.setFileName(fileName);
				item.setTitle(title);
				item.setPubDate(pubDate);
				item.setRead(isRead > 0 ? true : false);
				item.setFavorite(isFavor > 0 ? true : false);
				item.setAttach(attach);
				item.setChannelTitle(channelTitle);
				item.setChannelFileName(channelFileName);
				items.addElement(item);
			}
			cursor.close();
			db.close();
			return items;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			e.printStackTrace();
			db.close();
			return null;
		}
	}
    /*
     * 获得已收藏的新闻列表
     */
	public static Vector getFavorNews() {
		DataBase db=new DataBase();
		db.open();
		Cursor cursor = db.getFavorNews();
		Vector items = new Vector();
		Row row;
		try {
			while (cursor.next()) {
				row = cursor.getRow();
				String id = row.getString(0); 
				String fileName = row.getString(1);
				String title = row.getString(2);
				String pubDate = row.getString(3);
				int isRead = row.getInteger(4);
				String attach = row.getString(5);
				String channelTitle = row.getString(6);
				String channelFileName = row.getString(7);
				int isFavor = row.getInteger(8);
				NewsItem item = new NewsItem();
				item.setID(id);
				item.setFileName(fileName);
				item.setTitle(title);
				item.setPubDate(pubDate);
				item.setRead(isRead > 0 ? true : false);
				item.setFavorite(isFavor > 0 ? true : false);
				item.setAttach(attach);
				item.setChannelTitle(channelTitle);
				item.setChannelFileName(channelFileName);
				items.addElement(item);
			}
			cursor.close();
			db.close();
			return items;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			e.printStackTrace();
			db.close();
			return null;
		}
	}
    /*
     * 获得指定频道的所有新闻
     */
	public static Vector getNewsByChannel(String channelFileName) {
		DataBase db=new DataBase();
		db.open();
		Cursor cursor = db.getNewsByChannel(channelFileName);
		Vector items = new Vector();
		Row row;
		try {
			while (cursor.next()) {
				row = cursor.getRow();
				String id = row.getString(0);
				String fileName = row.getString(1);
				String title = row.getString(2);
				String pubDate = row.getString(3);
				int isRead = row.getInteger(4);
				String attach = row.getString(5);
				int isFavor = row.getInteger(7);
				NewsItem item = new NewsItem();
				item.setID(id);
				item.setFileName(fileName);
				item.setTitle(title);
				item.setPubDate(pubDate);
				item.setRead(isRead > 0 ? true : false);
				item.setFavorite(isFavor > 0 ? true : false);
				item.setAttach(attach);
				item.setChannelFileName(channelFileName);
				items.addElement(item);
			}
			cursor.close();
			db.close();
			return items;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			e.printStackTrace();
			db.close();
			return null;
		}
	}
	public static Vector getVideoItems(String channelFileName) {
		DataBase db=new DataBase();
		db.open();
		Cursor cursor = db.getVideoItems(channelFileName);
		Vector items = new Vector();
		Row row;
		try {
			while (cursor.next()) {
				row = cursor.getRow();
				String id = row.getString(0);
				String fileName = row.getString(1);
				String title = row.getString(2);
				String pubDate = row.getString(3);
				int isRead = row.getInteger(4);
				String attach = row.getString(5);
				int isFavor = row.getInteger(6);
				NewsItem item = new NewsItem();
				item.setID(id);
				item.setFileName(fileName);
				item.setTitle(title);
				item.setPubDate(pubDate);
				item.setRead(isRead > 0 ? true : false);
				item.setFavorite(isFavor > 0 ? true : false);
				item.setAttach(attach);
				item.setChannelFileName(channelFileName);
				items.addElement(item);
			}
			cursor.close();
			db.close();
			return items;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			e.printStackTrace();
			db.close();
			return null;
		}
	}
	/*
	 * 根据ID获得新闻内容
	 */
	public static String getNewsItemContent(String id){
		DataBase db=new DataBase();
		db.open();
		String content = db.getItemContent(id);
		db.close();
		return content;
	}
	/*
	 * 修改指定ID的新闻内容
	 */
	public static boolean setNewsItemContent(String id,String content){
		DataBase db=new DataBase();
		db.open();
		boolean res = db.setItemContent(id,content);
		db.close();
		return res;
	}
    /*
     * 根据ID获得新闻
     */
	public static NewsItem getNewsByID(String id) {
		DataBase db=new DataBase();
		db.open();
		Cursor cursor = db.getNewsByID(id);
		NewsItem item = null;
		Row row;
		try {
			if (cursor.next()) {
				row = cursor.getRow();
				String fileName = row.getString(1);
				String title = row.getString(2);
				String pubDate = row.getString(3);
				int isRead = row.getInteger(4);
				String attach = row.getString(5);
				String channelFileName = row.getString(6);
				int isFavor = row.getInteger(7);
				item = new NewsItem();
				item.setID(id);
				item.setFileName(fileName);
				item.setTitle(title);
				item.setPubDate(pubDate);
				item.setRead(isRead > 0 ? true : false);
				item.setFavorite(isFavor > 0 ? true : false);
				item.setAttach(attach);
				item.setChannelFileName(channelFileName);
			}
			cursor.close();
			db.close();
			return item;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			e.printStackTrace();
			db.close();
			return null;
		}
	}
	/*
	 * 获得与关键字匹配成功的新闻游标
	 */
	public static Cursor getNewsItemMatches(String keyword){
		DataBase db=new DataBase();
		db.open();
			Cursor c = db.getNewsItemMatches(keyword);
			db.close();
			return c;
	}
	/*
	 * 获得与关键字匹配成功的新闻
	 */
	public static Vector getNewsMatches(String keyword) {
		DataBase db=new DataBase();
		db.open();
		Cursor cursor = db.getNewsMatches(keyword);
		Vector items = new Vector();
		Row row;
		try {
			while (cursor.next()) {
				row = cursor.getRow();
				String id = row.getString(0); 
				String fileName = row.getString(1);
				String title = row.getString(2);
				String pubDate = row.getString(3);
				int isRead = row.getInteger(4);
				String attach = row.getString(5);
				String channelTitle = row.getString(6);
				String channelFileName = row.getString(7);
				int isFavor = row.getInteger(8);
				NewsItem item = new NewsItem();
				item.setID(id);
				item.setFileName(fileName);
				item.setTitle(title);
				item.setPubDate(pubDate);
				item.setRead(isRead > 0 ? true : false);
				item.setFavorite(isFavor > 0 ? true : false);
				item.setAttach(attach);
				item.setChannelTitle(channelTitle);
				item.setChannelFileName(channelFileName);
				items.addElement(item);
			}
			db.close();
			return items;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			db.close();
			return null;
		}
	}
	/*
	 * 更新联系人刷新的最后时间
	 */
	public static void updateContactLastTime(String id,String time){
		DataBase db=new DataBase();
		db.open();
		db.updateContactLastTime(id, time);
		db.close();
	}
	/*
	 * 插入新联系人
	 */
	public static boolean insertContacts(Contact c){
		DataBase db=new DataBase();
		db.open();
		boolean res=db.insertContact(c.getId(),c.getName(),c.getType(),c.getGroupId());
		db.close();
		return res;
	}
	/*
	 * 删除所有联系人
	 */
	public static void delAllContacts(){
		DataBase db=new DataBase();
		db.open();
		db.delAllContact();
		db.close();
	}
	/*
	 * 获得所有受到的信息
	 */
	public static Vector getMessages() {
		DataBase db=new DataBase();
		db.open();
		Cursor cursor = db.getMessages();
		Vector msgs = new Vector();
		Row row;
		try {
			while (cursor.next()) {
				row = cursor.getRow();
				String id = row.getString(0); 
				String title = row.getString(1);
				String date = row.getString(2);
				String sendername = row.getString(3);
				int isread = row.getInteger(4);
				MessageItem msg = new MessageItem();
				msg.setId(id);
				msg.setTitle(title);
				msg.setDate(date);
				msg.setSenderName(sendername);
				msg.setRead(isread > 0 ? true : false);
				msgs.addElement(msg);
			}
			cursor.close();
			db.close();
			return msgs;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			e.printStackTrace();
			db.close();
			return null;
		}
	}
	/*
	 * 根据ID删除信息
	 */
	public static boolean delMessage(String id) {
		DataBase db=new DataBase();
		db.open();
		db.delMessage(id);
		db.close();
		return true;
	}
	/*
	 * 删除所有已读信息
	 */
	public static boolean delAllReadMessage() {
		DataBase db=new DataBase();
		db.open();
		db.delAllReadMessage();
		db.close();
		return true;
	}
	/*
	 * 删除所有信息
	 */
	public static boolean delAllMessage() {
		DataBase db=new DataBase();
		db.open();
		db.delAllMessage();
		db.close();
		return true;
	}
	/*
	 * 根据ID设置信息为已读状态
	 */
	public static boolean setMessageItemRead(String msgId, boolean b) {
		DataBase db=new DataBase();
		db.open();
		boolean res = db.setMessageItemRead(msgId, b);
		db.close();
		return res;
	}
	/*
	 * 插入新信息
	 */
	public static boolean insertMessage(MessageItem msg){
		DataBase db=new DataBase();
		db.open();
		boolean res=db.insertMessage(msg.getId(),msg.getTitle(),msg.getSenderName(),msg.getDate(),msg.getAttach());
		db.close();
		return res;
	}
	/*
	 * 根据ID获得信息的正文
	 */
	public static String getMessageContent(String id){
		DataBase db=new DataBase();
		db.open();
		String content = db.getMessageContent(id);
		db.close();
		return content;
	}
	/*
	 * 根据ID更改新闻正文
	 */
	public static boolean setMessageContent(String id,String content){
		DataBase db=new DataBase();
		db.open();
		boolean res = db.setMessageContent(id,content);
		db.close();
		return res;
	}
	/*
	 * 获得所有联系人
	 */
	public static Vector getAllContacts() {
		DataBase db=new DataBase();
		db.open();
		Cursor cursor = db.getAllContacts();
		Vector contacts = new Vector();
		Row row;
		try {
			while (cursor.next()) {
				row = cursor.getRow();
				String id = row.getString(0);
				String name = row.getString(1);
				String type = row.getString(3);
				String groupid = row.getString(2);
				Contact c = new Contact();
				c.setId(id);
				c.setName(name);
				c.setType(type);
				c.setGroupId(groupid);
				contacts.addElement(c);
			}
			cursor.close();
			db.close();
			return contacts;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			e.printStackTrace();
			db.close();
			return null;
		}

	}
    /*
     * 根据城市ID更新天气
     */
	public static void updateWeathers(Vector weathers,int cityCode){
		DataBase db=new DataBase();
		db.open();
		int length=weathers.size();
		for(int i=0;i<length;i++) {
			WeatherItem item = (WeatherItem)weathers.elementAt(i);
			db.insertWeatherItem(item.getDate(),item.getTemperature(),item.getWind(),item.getWeatherDescription(),item.getWeatherPicture1(),item.getWeatherPicture2(),cityCode);
		}		
		db.close();
	}
    /*
     * 根据城市代码获取最新天气
     */
	public static Vector getWeathers(int citycode) {
		DataBase db=new DataBase();
		db.open();
		Cursor cursor = db.getWeathers(citycode);
		Vector items = new Vector();
		Row row;
		try {
			while (cursor.next()) {
				row = cursor.getRow();
				String date = row.getString(0);
				String temp = row.getString(1);
				String wind = row.getString(2);
				String des = row.getString(3);
				String weatherPic1 = row.getString(4);
				String weatherPic2 = row.getString(5);
				WeatherItem item = new WeatherItem();
				item.setDate(date);
				item.setTemperature(temp);
				item.setWeatherDescription(des);
				item.setWind(wind);
				item.setWeatherPicture1(weatherPic1);
				item.setWeatherPicture2(weatherPic2);
				items.addElement(item);
			}
			cursor.close();
			db.close();
			return items;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			e.printStackTrace();
			db.close();
			return null;
		}
	}
	/*
	 * 根据城市代码获取今天的天气
	 */
	public static WeatherItem getTodayWeather(int citycode){
		Vector weathers=getWeathers(citycode);
		WeatherItem weather = (WeatherItem)weathers.elementAt(2);
		return weather;
	}
	/*
	 * 插入省份
	 */
	public static boolean insertProvince(Province p){
		DataBase db=new DataBase();
		db.open();
		boolean res=db.insertProvince(p.getId(),p.getName());
		db.close();
		return res;
	}
    /*
     * 插入城市
     */
	public static boolean insertCity(City c){
		DataBase db=new DataBase();
		db.open();
		boolean res=db.insertCity(c.getId(),c.getName(),c.getCode(),c.getProvinceID());
		db.close();
		return res;
	}
    /*
     * 获得所有省份
     */
	public static Vector getProvinces() {
		DataBase db=new DataBase();
		db.open();
		Cursor cursor = db.getProvinces();
		Vector items = new Vector();
		Row row;
		try {
			while (cursor.next()) {
				row = cursor.getRow();
				int id = row.getInteger(0); 
				String name = row.getString(1);
				Province p = new Province();
				p.setId(id);
				p.setName(name);
				items.addElement(p);
			}
			cursor.close();
			db.close();
			return items;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			db.close();
			return null;
		}
	}
    /*
     * 根据省份ID获得该省的所有城市
     */
	public static Vector getCities(int provinceID) {
		DataBase db=new DataBase();
		db.open();
		Cursor cursor = db.getCities(provinceID);
		Vector items = new Vector();
		Row row;
		try {
			while (cursor.next()) {
				row = cursor.getRow();
				int id = row.getInteger(0); 
				String name = row.getString(1);
				int code = row.getInteger(2);			
				City c = new City();
				c.setId(id);
				c.setName(name);
				c.setCode(code);
				c.setProvinceID(provinceID);
				items.addElement(c);
			}
			cursor.close();
			db.close();
			return items;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			db.close();
			return null;
		}
	}
	/*
	 * 获得指向所有省份的游标
	 */
	public static Cursor getProvincesCursor(){
		DataBase db=new DataBase();
		db.open();
		Cursor provinces = db.getProvinces();
		db.close();
		return provinces;
	}
	/*
	 * 根据省份ID获得指向该省所有城市的游标
	 */
	public static Cursor getCitiesCursor(int provinceID){
		DataBase db=new DataBase();
		db.open();
		Cursor cities = db.getCities(String.valueOf(provinceID));
		db.close();
		return cities;
	}
    /*
     * 根据城市名称获得城市对象
     */
	public static City getCityByName(String name) {
		DataBase db=new DataBase();
		db.open();
		Cursor cursor = db.getCityByName(name);
		City city=null;
		Row row;
		try {
			if (cursor.next()) {
				row = cursor.getRow();
				int id = row.getInteger(0);
				int code = row.getInteger(2);
				int provinceID = row.getInteger(3);
				city=new City();
				city.setId(id);
				city.setName(name);
				city.setCode(code);
				city.setProvinceID(provinceID);
			}
			cursor.close();
			db.close();
			return city;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			db.close();
			return null;
		}
	}

   /*
    * 根据频道ID删除该频道的所有RSS条目	
    */
	public static void delRssItemsOfChannelID(String channelID){
		DataBase db=new DataBase();
		db.open();
		db.delRssItemsByChannelID(channelID);
		db.close();
	}
    /*
     * 获得所有RSS频道
     */
	public static Vector getAllRssChannels() {
		DataBase db=new DataBase();
		db.open();
		Cursor cursor = db.getAllRssChannels();
		Vector channels = new Vector();
		Row row;
		try {
			while (cursor.next()) {
				row = cursor.getRow();
				int id = row.getInteger(0);
				String link = row.getString(1);
				String name = row.getString(2);
				String keycontain = row.getString(3);
				String keynotcontain = row.getString(4);
				int searchregion = row.getInteger(5);
				RssChannel ch = new RssChannel();
				ch.setId(String.valueOf(id));
				ch.setLink(link);
				ch.setName(name);
				ch.setKeycontain(keycontain);
				ch.setKeynotcontain(keynotcontain);
				ch.setSearchregion(searchregion);
				channels.addElement(ch);
			}
			cursor.close();
			db.close();
			return channels;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			e.printStackTrace();
			db.close();
			return null;
		}
	}
	/*
	 * 根据RSS频道对象插入新的RSS频道
	 */
	public static String appendRssChannel(RssChannel ch){
		DataBase db=new DataBase();
		db.open();
		String id=db.insertRssChannel(ch);
		db.close();
		return id;
	}
	/*
	 * 根据RSS频道对象修改已有的RSS频道
	 */
	public static void updateRssChannel(RssChannel ch){
		DataBase db=new DataBase();
		db.open();
		
		db.updateRssChannel(Integer.parseInt(ch.getId()),ch.getName(),ch.getKeycontain(),ch.getKeynotcontain(),ch.getSearchregion(),ch.getLink());
		db.close();
	}
	/*
	 * 根据ID删除RSS频道
	 */
	public static void delRssChannelById(String id){
		DataBase db=new DataBase();
		db.open();
		db.delRssChannelById(id);
		db.close();
	}
    /*
     * 根据频道ID获得所有RSS条目
     */
	public static Vector getRssByChannel(String channelId) {
		DataBase db=new DataBase();
		db.open();
		Cursor cursor = db.getRssByChannel(channelId);
		Vector channels = new Vector();
		Row row;
		try {
			while (cursor.next()) {
				row = cursor.getRow();
				String id = String.valueOf(row.getInteger(0));
				String title = row.getString(1);
				String link = row.getString(2);
				int isRead = row.getInteger(3);
				String description = row.getString(4);
				String pubDate = row.getString(5);
				String source = row.getString(6);
				String author = row.getString(7);
				RssItem item = new RssItem();
				item.setID(String.valueOf(id));
				item.setTitle(title);
				item.setLink(link);
				item.setRead(isRead > 0 ? true : false);
				item.setDescription(description);
				item.setPubDate(pubDate);
				item.setSource(source);
				item.setAuthor(author);
				item.setChannelId(channelId);
				channels.addElement(item);
			}
			cursor.close();
			db.close();
			return channels;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			e.printStackTrace();
			db.close();
			return null;
		}
	}
     /*
      * 根据RSS条目列表、频道ID、保存的最多数量，插入RSS条目列表
      */
	public static void insertRssItems(Vector items, String channelID,int limit) {
		DataBase db=new DataBase();
		db.open();
		int length = items.size();
		for (int i = 0; i < length; i++) {
			RssItem item = (RssItem) items.elementAt(i);
			item.setChannelId(channelID);
			db.insertRssItem(item.getTitle(), item.getLink(), item
					.getDescription(), item.getPubDate(), item.getSource(),
					item.getAuthor(), item.getChannelId());
		}
		// 删除多余资讯
		int currentItemCount = db.getChannelRssItemCount(channelID);
		if (limit < currentItemCount) {
			// 需要删除旧的数据
			int del_cnt = currentItemCount - limit;
			Cursor cursor = db.getToDeleteRssItems(channelID, del_cnt);
			Row row;
			Vector rssToDelList = new Vector();
			try {
				while (cursor.next()) {
					row = cursor.getRow();
					String id = row.getString(0); //
					RssItem item = new RssItem();
					item.setID(id);
					rssToDelList.addElement(item);
				}
			} catch (Exception e) {
				RaspberryLogger.getInstance().info(e.getMessage());
				e.printStackTrace();
			}
			length = rssToDelList.size();
			if (rssToDelList != null && length != 0) {
				for (int i = 0; i < length; i++) {
					RssItem item = (RssItem) rssToDelList.elementAt(i);
					// 删除数据库
					db.delRssItemsByItemID(item.getID());
				}
			}
		}
		db.close();
	}
    /*
     * 根据ID获得RSS条目
     */
	public static RssItem getRssItemByID(String ID) {
		DataBase db=new DataBase();
		db.open();
		Cursor cursor = db.getRssItemById(ID);
		RssItem item = new RssItem();
		Row row;
		try {
			if (cursor.next()) {
				row = cursor.getRow();
				String title = row.getString(0);
				String link = row.getString(1);
				int isRead = row.getInteger(2);
				String description = row.getString(3);
				String pubDate = row.getString(4);
				String source = row.getString(5);
				String author = row.getString(6);
				String channelId = String.valueOf(row.getInteger(7));
				item.setTitle(title);
				item.setLink(link);
				item.setRead(isRead > 0 ? true : false);
				item.setDescription(description);
				item.setPubDate(pubDate);
				item.setSource(source);
				item.setAuthor(author);
				item.setChannelId(channelId);
			}
			cursor.close();
			db.close();
			return item;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			e.printStackTrace();
			db.close();
			return null;
		}
	}
	/*
	 * 根据ID设置RSS条目为已读状态
	 */
	public static void setRssItemRead(String id,boolean read){
		DataBase db=new DataBase();
		db.open();
		db.setRssItemRead(id, read);
		db.close();
	}
}
